﻿using System;
using System.Linq;
using System.Web.UI;
using EasyWeixin.CommonAPIs;
using EasyWeixin.Entities.JsonResult;

using RaceList.activity.help;
using RaceList.activity.util;

namespace RaceList.activity.relay
{
    /// <summary>
    ///     ?relay=first&cycle=123
    ///     ?relay=123
    /// </summary>
    public partial class Relaygame : Page, IAuthWeixin
    {
        protected Activity_Relay Relay { get; set; }

        protected WxConfigResult WxConfig { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            SessionHelp.UserChengji.Dispose();
            string cycleId = Request.QueryString[Parameters.cycle];
            string relayId = Request.QueryString[Parameters.relay];

            if (relayId == Parameters.first) //每个人第一次玩
            {
                #region

                if (string.IsNullOrEmpty(cycleId))
                {
                    Response.Redirect404(Request.Url.ToString());
                }
                else
                {
                    int id = Convert.ToInt32(cycleId);
                    Activity_Cycle cycle = RelayQuery.GetCycle(id);
                    if (cycle != null)
                    {
                        SessionHelp.CycleId = id;
                    }
                    else
                    {
                        Response.Redirect404(Request.Url.ToString());
                    }
                }

                #endregion
            }
            else //接着别人跑
            {
                if (string.IsNullOrEmpty(relayId))
                {
                    Response.Redirect404(Request.Url.ToString());
                }
                else
                {
                    Activity_Relay relay = RelayQuery.GetRelay(Convert.ToInt32(relayId));
                    if (relay != null)
                    {
                        SessionHelp.CycleId = relay.CycleId;
                        Relay = relay;
                        if (CheckCanHelp())
                        {
                            AddHelpSession(relay);
                        }
                        else
                        {
                            string resultUrl = "Relayresult.aspx?" + Parameters.relay + "=" + Relay.Id;
                            Response.Write(
                                string.Format(
                                    "<html><head></head><script>window.location.href='{0}'</script><body></body></html>"
                                    , resultUrl));
                            Response.End();
                        }
                    }
                    else
                    {
                        Response.Redirect404(Request.Url.ToString());
                    }
                }
            }

            InitWxConfig();
        }

        public void InitWxConfig()
        {
            WxConfigResult result = CommonApi.GetWxConfigResult(ActivityConfig.MHalloweenAppId,
                ActivityConfig.MHalloweenAppSecrect, Request.Url.ToString());
            WxConfig = result;
        }

        private bool CheckCanHelp()
        {
            int loginwxid = SessionHelp.UserData.WeixinUserId;

            bool canhelp = loginwxid != Relay.WeixinUserId; //如果相等的话，就是自己登录以后查看自己的也不能帮忙
            if (canhelp) //如果==true
            {
                canhelp = Relay.Activity_SubRelay.Count() + 1 < ActivityConfig.MHalloweenTotalPerson; //参与游戏的人少于ji人为true    
                if (canhelp)
                    canhelp = RelayQuery.GetCanHelp(loginwxid, Relay);
            }
            return canhelp;
        }

        private void AddHelpSession(Activity_Relay relay)
        {
            SessionHelp.UserChengji = new UserChengji
            {
                IsHelp = true,
                RelayId = relay.Id,
            };
        }
    }
}